﻿using DoNet.Bpmn.Core.Enums;
using DoNet.Bpmn.Core.Utils;
using DoNet.Bpmn.Core.Vo;
using DoNet.Bpmn.Models;
using DoNet.Bpmn.Services;
using Microsoft.Extensions.Logging;

namespace DoNet.Bpmn.Adaptor.BpmnNode
{
    public class NodePropertyBusinessTableAdaptor : BpmnNodeAdaptor
    {
        private readonly BpmnNodeBusinessTableConfigService _bpmnNodeBusinessTableConfigService;
        private readonly ILogger<NodePropertyBusinessTableAdaptor> _logger;

        public NodePropertyBusinessTableAdaptor(
            BpmnNodeBusinessTableConfigService bpmnNodeBusinessTableConfService,
            ILogger<NodePropertyBusinessTableAdaptor> logger)
        {
            _bpmnNodeBusinessTableConfigService = bpmnNodeBusinessTableConfService;
            _logger = logger;
        }

        public override BpmnNodeVo? FormatToBpmnNodeVo(BpmnNodeVo bpmnNodeVo)
        {
            var bpmnNodeBusinessTableConf = _bpmnNodeBusinessTableConfigService.GetByBpmnNodeId(bpmnNodeVo.Id);

            if (bpmnNodeBusinessTableConf != null)
            {
                bpmnNodeVo.Property = new BpmnNodePropertysVo
                {
                    ConfigurationTableType = bpmnNodeBusinessTableConf.ConfigurationTableType,
                    TableFieldType = bpmnNodeBusinessTableConf.TableFieldType,
                    SignType = bpmnNodeBusinessTableConf.SignType
                };
            }

            return bpmnNodeVo;
        }

        public override void EditBpmnNode(BpmnNodeVo bpmnNodeVo)
        {
            var bpmnNodePropertysVo = bpmnNodeVo.Property ?? new BpmnNodePropertysVo();

            var bpmnNodeBusinessTableConfig = new BpmnNodeBusinessTableConfig
            {
                BpmnNodeId = bpmnNodeVo.Id,
                ConfigurationTableType = bpmnNodePropertysVo.ConfigurationTableType,
                TableFieldType = bpmnNodePropertysVo.TableFieldType,
                SignType = bpmnNodePropertysVo.SignType,
                CreatorTime = DateTime.Now,
                //CreatorUserId = SecurityUtils.GetLogInEmpName(),
                LastModifyTime = DateTime.Now,
                //LastModifyUserId = SecurityUtils.GetLogInEmpName()
            };

            _bpmnNodeBusinessTableConfigService.Insert(bpmnNodeBusinessTableConfig);
        }

        public override void SetSupportBusinessObjects()
        {
            ((IAdaptorService)this).AddSupportBusinessObjects(BpmnNodeAdpConfigEnum.ADP_CONF_NODE_PROPERTY_BUSINESSTABLE);
        }
    }
}
